package com.ipad.ui.fragmen;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.cooltechworks.views.shimmer.ShimmerRecyclerView;
import com.ipad.R;
import com.ipad.bean.User;
import com.ipad.bean.Warehouse;
import com.ipad.net.entity.LoginEntity;
import com.ipad.net.entity.UserInfo;
import com.ipad.net.entity.WarehouseList;
import com.ipad.net.presenter.UserPresent;
import com.ipad.net.view.IUserView;
import com.ipad.ui.fragmen.adapter.WareHouseListV2Adapter;
import com.ipad.utils.CodeConfig;
import com.ipad.utils.SharePerenceUtils;
import com.ipad.utils.ToasUtils;
import com.ipad.view.dialog.MyListView;
import com.scwang.smartrefresh.header.DropboxHeader;
import com.scwang.smartrefresh.header.FlyRefreshHeader;
import com.scwang.smartrefresh.header.fungame.FunGameBase;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.FalsifyFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class WarehousV2Fragment extends DialogFragment implements IUserView {
    @BindView(R.id.warehouse_title_v2)
    TextView warehouse_title_v2;
    @BindView(R.id.warehouse_refresh)
    RefreshLayout warehouse_refresh;
    @BindView(R.id.warhouse_list_fragment_loading_v2)
    ProgressBar loading;
    @BindView(R.id.warehouse_fragment_dailog_list_v2)
    MyListView warehouse_list;
    @BindView(R.id.warehouse_fragment_v2_scan)
    EditText warehouse_fragment_v2_scan;

    List<Warehouse> warehouses = new ArrayList<>();
    WareHouseListV2Adapter v2Adapter;
    UserPresent userPresent;
    OnSelectWarehouse onSelectWarehouse;
    int pageNum = 1;
    int size = 10;
    int totalPage = 1;
    String name = "";
    private boolean isCreated = false;
    private String TAG = "warehouse";
    private String type;

    public static WarehousV2Fragment newInstance(int request_code) {

        Bundle args = new Bundle();
        args.putInt(CodeConfig.flag, request_code);
        WarehousV2Fragment fragment = new WarehousV2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    public WarehousV2Fragment() {
        // Required empty public constructor
        this.setStyle(STYLE_NO_TITLE, 0);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_warehous_v2, container, false);
        ButterKnife.bind(this, inflate);
        warehouse_title_v2.requestFocus();
        init();
        return inflate;
    }

    public void init() {
        type = getArguments().getInt(CodeConfig.flag)+"";
        onSelectWarehouse = (OnSelectWarehouse) getActivity();
        userPresent = new UserPresent();
        userPresent.attchView(this);

        v2Adapter = new WareHouseListV2Adapter(warehouses, getActivity());
        warehouse_list.setAdapter(v2Adapter);


        warehouses.clear();
        v2Adapter.notifyDataSetChanged();

        getWareHouseList(pageNum + "", size + "", "");

        warehouse_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                warehouses.clear();
                v2Adapter.notifyDataSetChanged();
                pageNum = 1;
                getWareHouseList(pageNum + "", size + "", "");
            }
        });
        warehouse_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                pageNum++;
                getWareHouseList(pageNum + "", size + "", "");

            }
        });

        warehouse_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onSelectWarehouse.selectWarehouse(warehouses.get(position));
                dismiss();
            }
        });

        warehouse_fragment_v2_scan.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
//                if (str != null && !str.equals("")){
                    pageNum = 1;
                    warehouses.clear();
                    v2Adapter.notifyDataSetChanged();
                    pageNum = 1;
                    getWareHouseList(pageNum + "", size + "", str);
//                }
            }
        });

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    public void getWareHouseList(String pageNumber, String pageSize, String name) {
        if (userPresent != null) {
            userPresent.getWarehouseList(type,SharePerenceUtils.getToken(getActivity()), pageNumber, pageSize, name, false);
        }
    }


    @Override
    public void stopLoading() {
        if (warehouse_refresh.isRefreshing()) {
            warehouse_refresh.finishRefresh();
        }
        if (warehouse_refresh.isLoading()) {
            warehouse_refresh.finishLoadMore();
        }
        loading.setVisibility(View.GONE);
    }

    @Override
    public void loading(String msg) {
        if (loading.getVisibility() != View.VISIBLE){
            loading.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void error(String msg) {
        ToasUtils.getToast(msg, getActivity());
    }

    @Override
    public void getUserInfo(UserInfo info) {

    }

    @Override
    public void getUserError() {

    }

    @Override
    public void login(LoginEntity loginEntity) {

    }

    @Override
    public void getWarehouseList(WarehouseList warehouseList) {
        if (warehouseList != null) {
            if (warehouseList.getList() != null && warehouseList.getList().size() != 0) {
                warehouses.addAll(warehouseList.getList());
                totalPage = warehouseList.getTotalRow();
            }
        }
        v2Adapter.notifyDataSetChanged();
    }

    public interface OnSelectWarehouse {
        void selectWarehouse(Warehouse warehouse);
    }
}
